/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class InstallWizard {
	private Install install[] = null;
	private SettingsStorePropertiesFile store = null;
	private String title = null;
	private int cursor = 0;

	public InstallWizard(SettingsStorePropertiesFile store,String title,Install install[]) {
		this.store = store;
		this.install = install;
		this.title = title;
	}

	public void show() {
		// STEP THROUGH EACH PANEL
		for (;cursor < install.length;) {
			install[cursor].setProperties(store.properties);
			int result = new InstallWizardPanel(install[cursor]).getResult();
			switch (result) {
			case InstallWizardPanel.NEXT:
				cursor++;
				break;
			case InstallWizardPanel.PREV:
				cursor--;
				break;
			case InstallWizardPanel.CANCEL:
				if (JOptionPane.showConfirmDialog(
				           null,
				           "Are you sure you want to cancel the installation?",
				           "Can installation",
				           JOptionPane.YES_NO_OPTION) == 0) {
					System.exit(0);
				}
				break;
			}
			if (cursor < 0) {
				cursor = 0;
			}
			System.out.println("CURSOR = " + cursor);
			store.save();
		}
		System.out.println("Mark -2");
	}

	public static class InstallWizardPanel extends JDialog implements ActionListener{
		private int return_code = 0;
		private Install install = null;
		private JButton next = new JButton("Next"),
		                       prev = new JButton("Prev"),
		                              cancel = new JButton("Cancel");

		public final static int NEXT = 0;
		public final static int PREV = 1;
		public final static int CANCEL = -1;

		public InstallWizardPanel(Install install) {
			super((Frame)null,install.getTitle());
			setModal(true);
			this.install = install;
			JPanel main = new JPanel();
			main.setLayout(new BorderLayout());
			JPanel middle = new JPanel();
			middle.setBorder(BorderFactory.createLineBorder(Color.black));
			middle.setLayout(new BorderLayout());
			middle.add(install.getPane(),BorderLayout.CENTER);
			JPanel bottom = new JPanel();
			bottom.setLayout(new BorderLayout());
			bottom.add(cancel,BorderLayout.WEST);
			bottom.add(prev,BorderLayout.CENTER);
			bottom.add(next,BorderLayout.EAST);
			main.add(middle,BorderLayout.CENTER);
			main.add(bottom,BorderLayout.SOUTH);

			cancel.addActionListener(this);
			prev.addActionListener(this);
			next.addActionListener(this);

			getContentPane().add(main);
			setSize(new Dimension(640,400));
			setLocationRelativeTo(null);
		}

		public int getResult() {
			show();
			return return_code;
		}

		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == cancel) {
				return_code = CANCEL;
				setVisible(false);
			}
			if (event.getSource() == next) {
				try {
					install.test();
					return_code = NEXT;
					setVisible(false);
				} catch (Exception exception) {
					JOptionPane.showMessageDialog(null,exception.getMessage());
					Log.debug.out(exception);
				}
			}
			if (event.getSource() == prev) {
				try {
					return_code = PREV;
					setVisible(false);
				} catch (Exception exception) {
					JOptionPane.showMessageDialog(null,exception.getMessage());
					Log.debug.out(exception);
				}
			}
		}
	}

}

